﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SPCFMedicineInformer.DocumentTypes;
using Vega.USiteBuilder;
using umbraco.cms.businesslogic.web;
using SPCFMedicineInformer.Constants;
using umbraco.BusinessLogic;
using SPCFMedicineInformer.Helpers;

namespace SPCFMedicineInformer.Masterpages
{
    public partial class Admin_CommonConditionsListing : System.Web.UI.MasterPage
    {
        public static List<string> CommonConditionList { get; set; }
        public  List<Condition> AllConditions { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bool isNew = Request.QueryString["n"] != null ? Request.QueryString["n"].ToString() == "1" ? true : false : false;
                AllConditions = NodeLocator.GetNodesOfExactType<Condition>().OrderBy(a => a.Name).ToList();
                CommonConditionList = NodeLocator.GetNodesOfExactType<Condition>().Where(node => node.IsCommonCondition).ToList().Select(node => node.Id.ToString()).ToList();
                if (CommonConditionList.Count > 0)
                {
                    hiddenCommonCondition.Value = string.Join(",", CommonConditionList);
                }
                

                if (isNew)
                    FormHelper.ShowMessage(MessageType.Done, "Common Conditions Succesfully Saved!", MsgLiteral);
            }
            
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            Save();
            Response.Redirect("/admin/utilities/conditions-listing/common-conditions?n=1");
        }
        void Save()
        {
            var commonList = hiddenCommonCondition.Value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
            List<Condition> conditionList = NodeLocator.GetNodesOfExactType<Condition>().ToList();
            foreach (var condition in conditionList)
            {
                bool value = false;
                if (commonList.Contains(condition.Id.ToString()))
                {
                    value = true;
                }
                EditDoc(condition.Id, value);
            }
        }
        void EditDoc(int id,bool value)
        {
            Document d = new Document(id);
            if (d != null)
            {
                d.getProperty(UmbracoPropertyName.IS_COMMON_CONDITION).Value = value;
                d.Publish(User.GetUser(0));
                umbraco.library.UpdateDocumentCache(d.Id);
            }
        }
        
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("/admin/site-management");
        }
        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            Response.Redirect("/admin/utilities/conditions-listing/condition-entry/");
        }
    }
}